package so.wy.servicecenter.businessbed.api;

import cn.binarywang.wx.miniapp.api.WxMaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import so.wy.servicecenter.base.exception.ServiceException;
import so.wy.servicecenter.base.frame.api.AbstractHttp;
import so.wy.servicecenter.base.frame.api.httpexception.HttpExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

@RestController
@Scope("request")
@RequestMapping("/miniapp/code")
public class MiniAppCodeHttp extends AbstractHttp {

    @Autowired
    private WxMaService wxMaService;

    @HttpExceptionHandler
    @RequestMapping("/gen")
    public void createCode(String deviceSerial, HttpServletRequest request, HttpServletResponse response) throws ServiceException {
        String path = "pages/index/index";

        String sence = "dSerial=" + deviceSerial;

        try {
            byte[] qrcodeBytes = wxMaService.getQrcodeService().createWxaCodeUnlimitBytes(sence, path, 860,
                    true, null, false);

            // 设置contentType
            response.setContentType("image/png");

            // 写入response的输出流中
            OutputStream stream = response.getOutputStream();
            stream.write(qrcodeBytes);
            stream.flush();
            stream.close();
        }catch (Exception e){
            logger.error("生成二维码失败!");
        }


    }
}
